#include <stdio.h>
#include <string.h>
#include "wet.h"

#define MAX_LINE_SIZE 128

static char command_name[MAX_LINE_SIZE + 2],command_line[MAX_LINE_SIZE + 2];
static char* arguments;
static int qid, aid;
static int missing_argc, argc;
static char question[MAX_LINE_SIZE + 2], answer[MAX_LINE_SIZE + 2], username[MAX_LINE_SIZE + 2], tag[MAX_LINE_SIZE + 2], date[MAX_LINE_SIZE + 2];

static void goto_next_line(FILE* file) {
        while (!feof(file) && fgetc(file) != '\n');
}

static int check_command(char* expected_name) {
        return (!strcmp(command_name,expected_name));
}

static int check_arguments(int expected_argc) {
        return ((missing_argc = expected_argc - argc) <= 0);
}

static void illegalCommand() {
        printf("Illegal command!\n");
}

void parseInput() {
        while (!feof(stdin)) {
                if (fgets(command_line,MAX_LINE_SIZE + 2,stdin) == NULL) break;
                if (strlen(command_line) > MAX_LINE_SIZE) {
                        printf("Command too long, can not parse\n");
                        goto_next_line(stdin);
                }
                else {
                        missing_argc = 0;
                        sprintf(command_name,"");
                        sscanf(command_line,"%s",command_name);
                        arguments=command_line + strlen(command_name) + 1;
                        if (check_command(COMMAND_ADD_QUESTION)) {
                                argc = sscanf(arguments,"%s %s %s",question,username, date);
                                if (check_arguments(3)) {
                                        addQuestion(question,username, date);
                                } else {
                                        illegalCommand();
                                }
                        }
                        else if (check_command(COMMAND_EDIT_QUESTION)) {
                                argc = sscanf(arguments,"%d %s",&qid, question);
                                if (check_arguments(2)) {
                                        editQuestion(qid,question);
                                } else {
                                        illegalCommand();
                                }
                        }
                        else if (check_command(COMMAND_DELETE_QUESTION)) {
                                argc = sscanf(arguments,"%d",&qid);
                                if (check_arguments(1)) {
                                        deleteQuestion(qid);
                                } else {
                                        illegalCommand();
                                }
                        }
                        else if (check_command(COMMAND_ADD_TAG)) {
                                argc = sscanf(arguments,"%d %s",&qid,tag);
                                if (check_arguments(2)) {
                                        addTag(qid, tag);
                                } else {
                                        illegalCommand();
                                }
                        }
                        else if (check_command(COMMAND_REMOVE_TAG)) {
                                argc = sscanf(arguments,"%d %s",&qid,tag);
                                if (check_arguments(2)) {
                                        removeTag(qid, tag);
                                } else {
                                        illegalCommand();
                                }
                        }
                        else if (check_command(COMMAND_PRINT_TAGS)) {
                                argc = sscanf(arguments,"%d",&qid);
                                if (check_arguments(1)) {
                                        printTags(qid);
                                } else {
                                        illegalCommand();
                                }
                        }
                        else if (check_command(COMMAND_ADD_ANSWER)) {
                                argc = sscanf(arguments,"%d %s",&qid,answer);
                                if (check_arguments(2)) {
                                        addAnswer(qid,answer);
                                } else {
                                        illegalCommand();
                                }
                        }
                        else if (check_command(COMMAND_EDIT_ANSWER)) {
                                argc = sscanf(arguments,"%d %s",&aid, answer);
                                if (check_arguments(2)) {
                                        editAnswer(aid,answer);
                                } else {
                                        illegalCommand();
                                }
                        }
                        else if (check_command(COMMAND_DELETE_ANSWER)) {
                                argc = sscanf(arguments,"%d",&aid);
                                if (check_arguments(1)) {
                                        deleteAnswer(aid);
                                } else {
                                        illegalCommand();
                                }
                        }
                        else if (check_command(COMMAND_APPROVE_ANSWER)) {
                                argc = sscanf(arguments,"%d",&aid);
                                if (check_arguments(1)) {
                                        approveAnswer(aid);
                                } else {
                                        illegalCommand();
                                }
                        }
                        else if (check_command(COMMAND_FIND_ANSWERS)) {
                                argc = sscanf(arguments,"%d",&qid);
                                if (check_arguments(1)) {
                                        findAnswers(qid);
                                } else {
                                        illegalCommand();
                                }
                        }
                        else if (check_command(COMMAND_SEARCH_BY_TAG)) {
                                argc = sscanf(arguments,"%s",tag);
                                if (check_arguments(1)) {
                                        searchByTag(tag);
                                } else {
                                        illegalCommand();
                                }
                        }
                        else if (check_command(COMMAND_FIND_RELATED_TAGS)) {
                                argc = sscanf(arguments,"%s",tag);
                                if (check_arguments(1)) {
                                        findRelatedTags(tag);
                                } else {
                                        illegalCommand();
                                }
                        }
                        else if (check_command(COMMAND_FIND_RELATED_QUESTIONS)) {
                                argc = sscanf(arguments,"%d",&qid);
                                if (check_arguments(1)) {
                                        findRelatedQuestions(qid);
                                } else {
                                        illegalCommand();
                                }
                        }
                        else if (check_command(COMMAND_PRINT_STATISTICS)) {
								printStatistics();
                        }
                        else if (check_command(COMMAND_INTERESTING_TAGS)) {
								interestingTags();
                        }
                        else if (check_command(COMMAND_BEST_ANSWERS_FOR_TAG)) {
                                argc = sscanf(arguments,"%s",tag);
                                if (check_arguments(1)) {
										bestAnswersForTag(tag);
                               } else {
                                        illegalCommand();
                                }
                        }
                        else if (check_command(COMMAND_DELETE_REDUNDANT_ANSWERS)) {
								deleteRedundantAnswers();
                        }
                        else if (check_command(COMMAND_EXIT)) {
                                break;
                        }
						else if (check_command(COMMAND_RESET)) {
							RESET();
						} else if (missing_argc > 0) {
                                printf("Missing %d argument(s) in command %s!\n",missing_argc,command_name);
                        } else if (argc > 0) {
                                illegalCommand();
                        } else {
                                illegalCommand();
                        }
                }
        }
}
